package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szholly.plug.form.excel.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.FailureRepairService;
import com.szholly.pro.watergas_server.ps.service.MaterialReplaceService;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONObject;


@Controller
@RequestMapping("/FailureRepair/*")
public class OperationFailureRepairController extends BaseController{ 
	
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	FailureRepairService failureRepairService;
	
	@Autowired
	MaterialReplaceService materialReplaceService;
	
	
	/**
	 * 获取设备故障及维修记录
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getFailureRepairList")
	public void getFailureRepairList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params  = getParams();
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = new ArrayList<RowBase>();
		//地表水使用另外一个故障维修查询
		if("1".equals(params.get("water_type"))) {
			list = failureRepairService.getSWFailureRepairList(params, pageInfo);
		} else {
			list = failureRepairService.getFailureRepairList(params, pageInfo);
		}
		JSONObject json = DataUtil.listToJSONObject(list, null, pageInfo);
		String result = json.toString();
		outString(result);
	}
	
	
	/**
	 * 获取设备故障关联的异常记录
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getFailureException")
	public void getFailureException(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String strTASK_ID = RequestUnit.getString("TASK_ID");
		String strSQL = " select e.*, t1.c_userealname as  CREATE_BY_NAME, t2.c_userealname as SOLVE_BY_NAME,  "
				+ " d1.S_DOMAINCAPTION as EXCEPTION_TYPE_NAME, d2.S_DOMAINCAPTION as EXCEPTION_STATUS_NAME from "
				+ " ( select * from OPERATION_EXCEPTION t where t.SOLVE_TASK_CODE='"+strTASK_ID+"' ) e "
				+ " left join  safe_user t1 on ( t1.s_userid = e.CREATE_BY ) "
				+ " left join  safe_user t2 on ( t2.s_userid = e.SOLVE_BY ) "
				+ " left join DATA_DEFULATDOMAIN d1 on ( d1.S_DOMAINNAME = 'OPERATION_EXCEPTION_TYPE' AND d1.S_DOMAINCODE = e.EXCEPTION_TYPE ) "
				+ " left join DATA_DEFULATDOMAIN d2 on ( d2.S_DOMAINNAME = 'OPERATION_EXCEPTION_STATUS' AND d2.S_DOMAINCODE = e.EXCEPTION_STATUS ) "
				+ " order by e.CREATE_TIME DESC ";
		String result = DataUtil.getPageSelectJson(strSQL, null);
		outString(result);
	}
	
	
	/**
	 * 获取字典项
	 * wangl
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDataDomain")
	public void getJobList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String strDomainName = RequestUnit.getString("DomainName");
		String strSQL = " select * from DATA_DEFULATDOMAIN t where t.S_DOMAINNAME='"+strDomainName+"' ";
		String result = DataUtil.getPageSelectJson(strSQL, null);
		outString(result);
	}
	
	private Map<String,String> getParams(){
		Map<String, String> params = new HashMap<String, String>();
		
		String strTASK_ID = RequestUnit.getString("TASK_ID");
		if (StringUtil.IsNotNullOrEmpty(strTASK_ID)) {
			params.put("task_code", strTASK_ID);
		}
		
		String strWWTP_ID = RequestUnit.getString("WWTP_ID");
		if (StringUtil.IsNotNullOrEmpty(strWWTP_ID)) {
			params.put("wwtp_id", strWWTP_ID);
		}
		
		String strFLOW_NAME = RequestUnit.getString("FLOW_NAME");
		if (StringUtil.IsNotNullOrEmpty(strFLOW_NAME)) {
			params.put("flow_type", strFLOW_NAME);
		}
		
		String strSTATION_ID = RequestUnit.getString("STATION_ID");
		if (StringUtil.IsNotNullOrEmpty(strSTATION_ID)) {
			params.put("station_id", strSTATION_ID);
		}
		
		String water_type = RequestUnit.getString("WATER_TYPE");
		if (StringUtil.IsNotNullOrEmpty(water_type)) {
			params.put("water_type", water_type);
		}
		
		String strDEVICE_NUM = RequestUnit.getString("DEVICE_NUM");
		if (StringUtil.IsNotNullOrEmpty(strDEVICE_NUM)) {
			params.put("device_num", strDEVICE_NUM);
		}
		
		String STATION_NAME = RequestUnit.getString("STATION_NAME");
		if (StringUtil.IsNotNullOrEmpty(STATION_NAME)) {
			params.put("station_name", STATION_NAME);
		}
		
		String strDEVICE_STATUS = RequestUnit.getString("DEVICE_STATUS");
		if (StringUtil.IsNotNullOrEmpty(strDEVICE_STATUS)) {
			String mn = StringUtil.joinForSQL(strDEVICE_STATUS.split(","), ",");
			params.put("device_status", "(" + mn + ")");
		}
		
		String strFAILURE_REASON= RequestUnit.getString("FAILURE_REASON");
		if (StringUtil.IsNotNullOrEmpty(strFAILURE_REASON)) {
			String mn = StringUtil.joinForSQL(strFAILURE_REASON.split(","), ",");
			params.put("failure_reason", "(" + mn + ")");
		}
		
		String strREPAIR_RESULT = RequestUnit.getString("REPAIR_RESULT");
		if (StringUtil.IsNotNullOrEmpty(strREPAIR_RESULT)) {
			String mn = StringUtil.joinForSQL(strREPAIR_RESULT.split(","), ",");
			params.put("repaire_result", "(" + mn + ")");
		}
		
		String strBEGIN_TIME_FROM = RequestUnit.getString("BEGIN_TIME_FROM");
		if (StringUtil.IsNotNullOrEmpty(strBEGIN_TIME_FROM)) {
			params.put("start", strBEGIN_TIME_FROM);
		}
		
		String strBEGIN_TIME_TO = RequestUnit.getString("BEGIN_TIME_TO");
		if (StringUtil.IsNotNullOrEmpty(strBEGIN_TIME_TO)) {
			params.put("end", strBEGIN_TIME_TO);
		}
		return params;
	}

	/**
	 * 导出设备维修报表
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportDoc")
	public void exportDoc(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String, String> params = getParams();

		Map<String, Object> root = new HashMap<String, Object>();
		List<RowBase> list = failureRepairService.getFailureRepairList(params, DataUtil.getPageInfo());
		
		RowBase row = null;
		if(list!=null && list.size()>0){
			row = list.get(0);
			
			Date date = (Date)row.getValue("END_TIME");
			root.put("DATE", date);
			root.put("USER", row.getSafeString("REPAIRE_PERSON"));
			root.put("USER_NAME", row.getSafeString("USER_NAME"));
			root.put("ERROR_DESC", row.getSafeString("FAILURE_REASON_NAME"));
			root.put("ERROR_PROC", row.getSafeString(" REPAIR_METHOD"));
			root.put("ERROR_RESULT", row.getSafeString("REPAIR_RESULT_NAME"));
			root.put("ERROR_START_TIME", row.getValue("BEGIN_TIME"));
			root.put("ERROR_END_TIME", row.getValue("END_TIME"));
		}
		
		String OUNAME = "";
		if (params.containsKey("station_id")) {
			OUNAME = commonService.getOperationUnitByStaionId(params.get("station_id"));
		} else if (params.containsKey("wwtp_id")) {
			OUNAME = commonService.getOperationUnit(params.get("wwtp_id"));
		}
		root.put("OUNAME", OUNAME);
		
		list = materialReplaceService.getMaterialReplaceList(params, null);
		root.put("list", list);
		
		if (params.containsKey("station_id")) {
			row = commonService.getStationGroupInfo(params.get("station_id"));
			if (row != null) {
				root.put("GROUP_LEADER", row.getSafeString("GROUP_LEADER"));
			}
			root.put("STATION_NAME", params.get("station_name"));
		} else {
			row = commonService.getStation(params.get("wwtp_id"), params.get("flow_type"));
			if(row!=null){
				String stationName = row.getSafeString("NAME");
				String stationId = row.getSafeString("CODE");
				root.put("STATION_NAME",stationName);
				row = commonService.getStationGroupInfo(stationId);
				if(row!=null){
					root.put("GROUP_LEADER",row.getSafeString("GROUP_LEADER"));
				}
			}
		}
		outWord(root, "deviceRepaire.xml", "设备维修记录表.doc");
	}
	
	/**
	 * 导出设备维修报表(地表水)
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportSwDoc")
	public void exportSwDoc(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String, String> params = getParams();

		Map<String, Object> root = new HashMap<String, Object>();
		//故障维修记录查询
		List<RowBase> repairList = failureRepairService.getSWFailureRepairList(params, DataUtil.getPageInfo());
		
		RowBase row = null;
		if(repairList!=null && repairList.size()>0){
			row = repairList.get(0);
			
			Date date = (Date)row.getValue("END_TIME");
			root.put("DATE", date);
			root.put("USER", row.getSafeString("REPAIRE_PERSON"));
			root.put("USER_NAME", row.getSafeString("USER_NAME"));
			root.put("ERROR_DESC", row.getSafeString("FAILURE_REASON_NAME"));
			root.put("ERROR_PROC", row.getSafeString(" REPAIR_METHOD"));
			root.put("ERROR_RESULT", row.getSafeString("REPAIR_RESULT_NAME"));
			root.put("ERROR_START_TIME", row.getValue("BEGIN_TIME"));
			root.put("ERROR_END_TIME", row.getValue("END_TIME"));
		}
		
		String OUNAME = "";
		if (params.containsKey("station_id")) {
			OUNAME = commonService.getOperationUnitByStaionId(params.get("station_id"));
		} else if (params.containsKey("wwtp_id")) {
			OUNAME = commonService.getOperationUnit(params.get("wwtp_id"));
		}
		root.put("OUNAME", OUNAME);
		//耗材更换记录查询
		List<RowBase> list = materialReplaceService.getMaterialReplaceList(params, null);
		root.put("repairList", repairList);
		root.put("list", list);
		
		if (params.containsKey("station_id")) {
			row = commonService.getStationGroupInfo(params.get("station_id"));
			if (row != null) {
				root.put("GROUP_LEADER", row.getSafeString("GROUP_LEADER"));
			}
			root.put("STATION_NAME", params.get("station_name"));
		} else {
			row = commonService.getStation(params.get("wwtp_id"), params.get("flow_type"));
			if(row!=null){
				String stationName = row.getSafeString("NAME");
				String stationId = row.getSafeString("CODE");
				root.put("STATION_NAME",stationName);
				row = commonService.getStationGroupInfo(stationId);
				if(row!=null){
					root.put("GROUP_LEADER",row.getSafeString("GROUP_LEADER"));
				}
			}
		}
		outWord(root, "sw/deviceRepaire_sw.xml", "设备维修记录表.doc");
	}
}
